<?php
/**
 * Created by PhpStorm.
 * User: 11951
 * Date: 2019/7/17
 * Time: 23:38
 */

set_time_limit(0);

function p($data)
{
    echo "<pre>";
    print_r($data);
    exit();
}

class mysql
{
    public function batchInsert($data)
    {
        $table = '`u3drunfast_log`.`user_day_play0`';
        $field = '';
        $valueStr = '';
        $fieldKey = array_keys($data[0]);

        foreach ($fieldKey as $ifileld) {
            $field .= "`{$ifileld}`,";
        }
        $field = trim($field, ',');

        foreach ($data as $index => $datum) {
            $str = '';
            foreach ($fieldKey as $ifileld) {
                $str .= "'{$datum[$ifileld]}',";
            }
            $str = trim($str, ',');
            $str = "({$str}),";
            $valueStr .= $str;
        }
        $valueStr = trim($valueStr, ',');
        $insert = "INSERT INTO {$table} ({$field}) VALUES {$valueStr}";
        $this->exec($insert);
        return $insert;
    }

    public function exec($sql)
    {
        $db_ms = 'mysql';  //数据库类型
        $db_host = '127.0.0.1';  //主机地址
        $db_user = 'root';  //数据库账号
        $db_pass = 'led20131111';  //数据库密码
        $db_name = 'u3drunfast_log'; //数据库名
        //拼接数据库主机信息
        $dbh = $db_ms . ':host=' . $db_host . ';' . 'dbname=' . $db_name;
        try {
            //开始连接数据库
            $dbh = new PDO($dbh, $db_user, $db_pass);
            //echo '连接成功';
            //设置字符集
            $dbh->query('set names utf8');
            $dbh->query($sql);
        } catch (PDOException $e) {
            //连接失败错误提示
            die('error:' . $e->getMessage());
        }
    }
}

$time = time();
$mysql = new mysql();
for ($j = 0; $j < 1000; $j++) {
    $data = [];
    for ($i = 0; $i < 10000; $i++) {
        $data[] = [
            'id' => 1,
            'mid' => 2,
            'master' => 3,
            'date' => date('Y-m-d H:i:s'),
            'room_all' => 6,
            'player_all' => 7,
            'pay_all' => 8,
            'room_type' => 9,
            'colltype' => 10,
        ];
    }
    $insert = $mysql->batchInsert($data);
}

p([
    '花费时间：' => time() - $time
]);
